package view.interfaces;

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import Util.Constants;
import business.dao.ClienteDAO;
import business.model.ClienteTO;
import business.model.tableModel.ClienteTableModel;
import exception.BusinessException;
import exception.InterfaceException;
import exception.UsuarioException;

public class telaCliente extends JFrame {

	private JPanel contentPane;
	private JTextField txtNome;
	private JTextField textTelefone;
	private JTextField textEndereco;
	private JTextField textCep;
	private JTextField textTelefonePq;
	private JTable tblConsulta;
	private ClienteTableModel clienteTableModel;
	private ClienteDAO clienteDAO = new ClienteDAO();
	private JButton btnSalvar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					telaCliente frame = new telaCliente();
					frame.setResizable(false);
					frame.setVisible(true);
					frame.lista();
					frame.btnSalvar.setEnabled(false);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public telaCliente() {
		setTitle("Cliente");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 641, 422);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.setToolTipText("");

		JLabel lblCadastrarNovoUsurio = new JLabel(
				"Cadastrar Novo Usu\u00E1rio");
		lblCadastrarNovoUsurio.setFont(new Font("Tahoma", Font.BOLD, 14));

		JLabel lblEditarUsurio = new JLabel("Editar Usu\u00E1rio");
		lblEditarUsurio.setFont(new Font("Tahoma", Font.BOLD, 14));

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(new Color(0, 0, 0)));

		JButton btnNewButton_2 = new JButton("VOLTAR");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new telaAtendente().setVisible(true);
				setVisible(false);
			}
		});
		btnNewButton_2.setFont(new Font("Tahoma", Font.BOLD, 14));

		JLabel lblConsultarUsuriosCadastrados = new JLabel(
				"Consultar Usu\u00E1rios Cadastrados");
		lblConsultarUsuriosCadastrados
				.setFont(new Font("Tahoma", Font.BOLD, 14));

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setForeground(Color.WHITE);
		scrollPane.setBackground(Color.WHITE);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																lblEditarUsurio)
														.addComponent(
																panel_2,
																GroupLayout.DEFAULT_SIZE,
																183,
																Short.MAX_VALUE)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addGap(55)
																		.addComponent(
																				btnNewButton_2)))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																lblCadastrarNovoUsurio)
														.addComponent(
																panel_1,
																GroupLayout.PREFERRED_SIZE,
																408,
																GroupLayout.PREFERRED_SIZE))
										.addGap(609))
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGap(8)
										.addComponent(scrollPane, 300, 589,
												GroupLayout.PREFERRED_SIZE)
										.addContainerGap())
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												lblConsultarUsuriosCadastrados)
										.addContainerGap(975, Short.MAX_VALUE)));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblCadastrarNovoUsurio)
														.addComponent(
																lblEditarUsurio))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																panel_1,
																GroupLayout.PREFERRED_SIZE,
																179,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				panel_2,
																				GroupLayout.PREFERRED_SIZE,
																				110,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(18)
																		.addComponent(
																				btnNewButton_2)))
										.addPreferredGap(
												ComponentPlacement.RELATED,
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(
												lblConsultarUsuriosCadastrados)
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(scrollPane,
												GroupLayout.PREFERRED_SIZE,
												132, GroupLayout.PREFERRED_SIZE)
										.addContainerGap()));

		tblConsulta = new JTable();
		scrollPane.setViewportView(tblConsulta);
		tblConsulta.setAutoCreateRowSorter(true);
		tblConsulta.setAutoCreateColumnsFromModel(true);
		tblConsulta.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					ClienteTO to = clienteTableModel.cliente.get(tblConsulta
							.getSelectedRow());
					textCep.setText(to.getCep());
					txtNome.setText(to.getNome());
					textEndereco.setText(to.getEndereco());
					textTelefone.setText(to.getTelefone());
					btnSalvar.setEnabled(Boolean.TRUE);
				}

			}
		});

		JTextPane txtpnTelefone_1 = new JTextPane();
		txtpnTelefone_1.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtpnTelefone_1.setText("Telefone:");
		txtpnTelefone_1.setEditable(false);
		txtpnTelefone_1.setBackground(SystemColor.menu);

		textTelefonePq = new JTextField();
		textTelefonePq.setColumns(10);

		JButton btnNewButton_1 = new JButton("Editar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ClienteTO to = new ClienteTO();
				to.setTelefone(textTelefonePq.getText());
				try {
					to = clienteDAO.consultar(to);

					textCep.setText(to.getCep());
					txtNome.setText(to.getNome());
					textEndereco.setText(to.getEndereco());
					textTelefone.setText(to.getTelefone());
					btnSalvar.setEnabled(true);
				} catch (BusinessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UsuarioException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterfaceException e) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(rootPane, e.getMessage(),
							Constants.Telas.TELA_PRODUTOS,
							JOptionPane.OK_OPTION);
				}
			}
		});
		btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 11));
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2
				.setHorizontalGroup(gl_panel_2
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_2
										.createSequentialGroup()
										.addGap(19)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																txtpnTelefone_1,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																textTelefonePq,
																GroupLayout.PREFERRED_SIZE,
																152,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(20, Short.MAX_VALUE))
						.addGroup(
								Alignment.TRAILING,
								gl_panel_2.createSequentialGroup()
										.addContainerGap(65, Short.MAX_VALUE)
										.addComponent(btnNewButton_1)
										.addGap(63)));
		gl_panel_2.setVerticalGroup(gl_panel_2.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel_2
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(txtpnTelefone_1,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(textTelefonePq,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(btnNewButton_1)
						.addContainerGap(17, Short.MAX_VALUE)));
		panel_2.setLayout(gl_panel_2);

		txtNome = new JTextField();
		txtNome.setColumns(10);

		textTelefone = new JTextField();
		textTelefone.setColumns(10);

		textEndereco = new JTextField();
		textEndereco.setColumns(10);

		textCep = new JTextField();
		textCep.setColumns(10);

		JTextPane txtpnNome = new JTextPane();
		txtpnNome.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtpnNome.setEditable(false);
		txtpnNome.setBackground(SystemColor.control);
		txtpnNome.setText("Nome:");

		JTextPane txtpnTelefone = new JTextPane();
		txtpnTelefone.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtpnTelefone.setBackground(SystemColor.control);
		txtpnTelefone.setEditable(false);
		txtpnTelefone.setText("Telefone:");

		JTextPane txtpnEndereo = new JTextPane();
		txtpnEndereo.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtpnEndereo.setBackground(SystemColor.control);
		txtpnEndereo.setEditable(false);
		txtpnEndereo.setText("Endere\u00E7o");

		JTextPane txtpnCep = new JTextPane();
		txtpnCep.setFont(new Font("Tahoma", Font.BOLD, 11));
		txtpnCep.setBackground(SystemColor.control);
		txtpnCep.setEditable(false);
		txtpnCep.setText("CEP:");

		JButton btnNewButton = new JButton("Cadastrar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					ClienteDAO clienteDAO = new ClienteDAO();
					ClienteTO to = new ClienteTO();
					to.setCep(textCep.getText());
					to.setEndereco(textEndereco.getText());
					to.setNome(txtNome.getText());
					to.setTelefone(textTelefone.getText());

					clienteDAO.incluir(to);

					textCep.setText("");
					textEndereco.setText("");
					textTelefone.setText("");
					txtNome.setText("");
					textTelefonePq.setText("");

					lista();

				} catch (BusinessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UsuarioException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterfaceException e) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(rootPane, e.getMessage(),
							Constants.Telas.TELA_CLIENTE,
							e.getJOptionPaneType());
				}
			}
		});
		btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));

		btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ClienteTO to = new ClienteTO();
				to.setCep(textCep.getText());
				to.setEndereco(textEndereco.getText());
				to.setTelefone(textTelefone.getText());
				to.setNome(txtNome.getText());

				textCep.setText("");
				textEndereco.setText("");
				textTelefone.setText("");
				txtNome.setText("");
				textTelefonePq.setText("");

				btnSalvar.setEnabled(false);

				try {
					clienteDAO.alterar(to);
				} catch (BusinessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UsuarioException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterfaceException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				lista();
			}
		});
		btnSalvar.setEnabled(false);
		btnSalvar.setFont(new Font("Tahoma", Font.BOLD, 11));
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1
				.setHorizontalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addGroup(
																				gl_panel_1
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addGroup(
																								gl_panel_1
																										.createParallelGroup(
																												Alignment.TRAILING)
																										.addGroup(
																												gl_panel_1
																														.createSequentialGroup()
																														.addComponent(
																																txtpnNome,
																																GroupLayout.PREFERRED_SIZE,
																																GroupLayout.DEFAULT_SIZE,
																																GroupLayout.PREFERRED_SIZE)
																														.addGap(33))
																										.addGroup(
																												gl_panel_1
																														.createSequentialGroup()
																														.addComponent(
																																txtpnTelefone,
																																GroupLayout.PREFERRED_SIZE,
																																GroupLayout.DEFAULT_SIZE,
																																GroupLayout.PREFERRED_SIZE)
																														.addGap(18)))
																						.addComponent(
																								txtpnEndereo,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								txtpnCep,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE))
																		.addGap(13)
																		.addGroup(
																				gl_panel_1
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								txtNome,
																								Alignment.TRAILING,
																								GroupLayout.DEFAULT_SIZE,
																								293,
																								Short.MAX_VALUE)
																						.addComponent(
																								textEndereco,
																								GroupLayout.DEFAULT_SIZE,
																								293,
																								Short.MAX_VALUE)
																						.addComponent(
																								textTelefone,
																								GroupLayout.PREFERRED_SIZE,
																								152,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								textCep,
																								GroupLayout.PREFERRED_SIZE,
																								153,
																								GroupLayout.PREFERRED_SIZE))
																		.addContainerGap())
														.addGroup(
																Alignment.TRAILING,
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				btnNewButton,
																				GroupLayout.PREFERRED_SIZE,
																				170,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addComponent(
																				btnSalvar)
																		.addGap(26)))));
		gl_panel_1
				.setVerticalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				txtNome,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addComponent(
																				textTelefone,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				txtpnNome,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				txtpnTelefone,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				textEndereco,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addComponent(
																				textCep,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				txtpnEndereo,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addComponent(
																				txtpnCep,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)))
										.addPreferredGap(
												ComponentPlacement.RELATED, 12,
												Short.MAX_VALUE)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																btnNewButton)
														.addComponent(btnSalvar))
										.addContainerGap()));
		gl_panel_1.linkSize(SwingConstants.HORIZONTAL, new Component[] {
				btnNewButton, btnSalvar });
		panel_1.setLayout(gl_panel_1);
		contentPane.setLayout(gl_contentPane);
	}

	public void lista() {
		try {
			clienteTableModel = new ClienteTableModel(
					clienteDAO.listar(new ClienteTO()));
			tblConsulta.setEnabled(true);

			tblConsulta.setModel(clienteTableModel);
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UsuarioException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterfaceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
